package com.hxn.wxshake.web;

import com.hxn.wxshake.bean.Data;
import com.hxn.wxshake.bean.WebResult;
import com.hxn.wxshake.cache.TimeTest;
import com.hxn.wxshake.bean.ActivityTime;
import com.hxn.wxshake.constant.ModuleCodeInterval;
import com.hxn.wxshake.constant.SysErrorCodeEnum;
import com.hxn.wxshake.utils.ListSort;
import com.hxn.wxshake.utils.QRCodeUtil;
import com.hxn.wxshake.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.*;

/**
 * @ClassName IndexAPI
 * @Description 活动的api
 * @Author hxn
 * @Date 2019/7/24 13:51
 * @Version 1.0
 **/
@RestController
public class IndexAPI {

    @Autowired
    private RedisUtils redisUtils;

    public static String redisName = "activityList_";
    private final static int pageShowSize = 80;

    /**
     * @Author hxn
     * @Description 获取参加游戏的人数
     * @return
     **/
    @GetMapping("/getManNum.do")
    public WebResult getManNum(){
        WebResult res;
        try {
            res = WebResult.success(redisUtils.hSize(redisName));
        }catch (Exception e){
            e.printStackTrace();
            res = WebResult.fail(ModuleCodeInterval.indexErrorDefult,"从redis获取参加游戏的人数失败，请联系管理员",null);
        }
        return res;
    }

    /**
     * @Author hxn
     * @Description 获取参与游戏的人的列表，超过pageShowSize个之后全部都显示  ......
     * @return
     **/
    @GetMapping("/getManList.do")
    public WebResult getManList(){
        WebResult res;
        try {
            List<Data> data = ListSort.sortByValueByDescSubStringKey(redisUtils.hValues(redisName));
            if(data.size() > pageShowSize){
                data = data.subList(0,pageShowSize);
                data.add(new Data("......","0","OTHER"));
            }
            res = WebResult.success(data);
        }catch (Exception e){
            e.printStackTrace();
            res = WebResult.fail(ModuleCodeInterval.indexErrorDefult,"从redis获取参与游戏的人的列表失败，请联系管理员",null);
        }
        return res;
    }
    /**
     * @Author hxn
     * @Description 初始化游戏的设置
     * @Param time              游戏的开始时间
     * @Param activityLength    游戏的时常
     * @return
     **/
    @PostMapping("/initActivity.do")
    public WebResult initActivity(@Valid ActivityTime time,Integer activityLength){
        WebResult res;
        if(null == activityLength){
            res = WebResult.fail(SysErrorCodeEnum.MISSING_REQUEST_PARAM_ERROR.getCode(),SysErrorCodeEnum.MISSING_REQUEST_PARAM_ERROR.getMessage(),null);
        }else {
            try {
                //清空缓存
                redisUtils.delete(redisName);
                //设置时间
                TimeTest.setActivityLength(activityLength);
                TimeTest.setActivityTime(time.getYear(), time.getMonth(), time.getDate(), time.getHours(), time.getMinutes());
                res = WebResult.success("初始化成功");
            }catch (Exception e){
                res = WebResult.fail(ModuleCodeInterval.timeErrorDefult,"初始化游戏的设置失败，请联系系统管理员",null);
            }
        }
        return res;
    }

    /**
     * @Author hxn
     * @Description 获取游戏开始时间
     * @return
     **/
    @GetMapping("/getTime.do")
    public WebResult getTime(){
        WebResult res;
        try {
            res = WebResult.success(TimeTest.getTimeStr());
        }catch (Exception e){
            res = WebResult.fail(ModuleCodeInterval.indexErrorDefult,"获取游戏开始时间失败，请联系系统管理员",null);
        }
        return res;
    }

    /**
     * @Author hxn
     * @Description 获取游戏时常
     * @return
     **/
    @GetMapping("/getActivity.do")
    public WebResult getActivity(){
        WebResult res;
        try {
            res = WebResult.success(TimeTest.getActivityLength());
        }catch (Exception e){
            res = WebResult.fail(ModuleCodeInterval.activityErrorDefult,"获取游戏时常失败，请联系系统管理员",null);
        }
        return res;
    }

    /**
     * @Author hxn
     * @Description 摇一摇提交数据
     * @Param username      用户名
     * @Param openId        openId
     * @Param data          提交的次数
     * @Param gender        性别
     * @return
     **/
    @PostMapping("/updateData.do")
    public WebResult updateData(String username,String openId,Integer data,String gender){
        WebResult res;
        if(null == data || StringUtils.isEmpty(username)|| StringUtils.isEmpty(openId)|| StringUtils.isEmpty(gender)){
            res = WebResult.fail(SysErrorCodeEnum.MISSING_REQUEST_PARAM_ERROR.getCode(),SysErrorCodeEnum.MISSING_REQUEST_PARAM_ERROR.getMessage(),null);
        }else {
            try {
                redisUtils.hPut(redisName,openId ,new Data().getDataString(username,String.valueOf(data),gender));
                res = WebResult.success();
            }catch (Exception e){
                e.printStackTrace();
                res = WebResult.fail(ModuleCodeInterval.activityErrorDefult,"摇一摇提交数据失败，请联系系统管理员",null);
            }
        }
        return res;
    }

}
